define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数
            Table.api.init({
                extend: {
                    index_url: 'vactivities/ticket/index' + location.search,
                    add_url: 'vactivities/ticket/add',
                    //edit_url: 'vactivities/ticket/edit',
                    //del_url: 'vactivities/ticket/del',
                  //  multi_url: 'vactivities/ticket/multi',
                   // import_url: 'vactivities/ticket/import',
                    table: 'ticket',
                },
                search: true,
                commonSearch: false,
                searchFormVisible: false,
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        //{checkbox: true},
                        /*{field: 'id', title: __('Id')},*/
                        {field: 'name', title: __('活动名称'), operate: 'LIKE'},
                        {field: 'isticket', title: __('参与人选项'), operate:'BETWEEN'},
                        {field: 'totalNumber', title: __('门票总数')},
                        {field: 'totalTemp', title: __('剩余门票')},
                        {field: 'endTime', title: __('活动结束时间')},
                        /*{field: 'activity_id', title: __('Activity_id')},*/
                        {field: 'isLoop', title: __('周期性')},
                        {field: 'createTime', title: __('创建时间')},
                        {field: 'isend', title: __('售卖状态'), operate: false,formatter:function(value,row){
                            if (row['isend'] == 1)
                            {
                                return '<span style="color:red">关闭</span>';
                            }else if(row['isend'] == 0) {
                                return '<span style="color:green">开启</span>';
                            }
                        }},
                        {field: 'operate', title: __('Operate'),
                            table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons :[
                                {   name: 'execute',
                                    title: __('添加门票'),
                                    text: __('添加门票'),
                                    url: function (row){
                                        return 'vactivities/ticket/ticketadd?activity_id='+row['id'];
                                    },
                                   // icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-execute btn-dialog  btn-add',
                                    hidden:function(row){
                                        if(row['isend'] == 1  || (row['startTime'] < row['nowTime'])){
                                            return true;
                                        }
                                    },
                                },
                                {   name: 'execute',
                                    title: __('编辑门票'),
                                    text: __('编辑门票'),
                                    url: 'vactivities/ticket/ticketlist?activity_id={ids}',
                                    //icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-execute btn-dialog',
                                    hidden:function(row) {
                                        if (row['isend'] == 1 || (row['startTime'] < row['nowTime'])) {
                                            return true;
                                        }
                                    }
                                    },
                                {   name: 'execute',
                                    title: __('查看门票'),
                                    text: __('查看门票'),
                                    url: 'vactivities/ticket/ticketview?activity_id={ids}',
                                    //icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-execute btn-dialog',
                                    hidden:function(row) {
                                        if (row['isend'] == 0) {
                                            return true;
                                        }
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        ticketlist:function (){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'vactivities/ticket/ticketlist' + location.search,
                    add_url: 'vactivities/ticket/add',
                    //edit_url: 'vactivities/ticket/edit',
                    del_url: 'vactivities/ticket/del',
                    //multi_url: 'vactivities/ticket/multi',
                    import_url: 'vactivities/ticket/import',
                    table: 'activity_participants',
                },
                search: true,
                commonSearch: false,
                searchFormVisible: false,
            });

            var table = $("#tablelist");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder:'asc',
                columns: [
                    [
                        // {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'name', title: __('门票名称'), operate: 'LIKE'},
                        {field: 'price', title: __('门票价格')},
                        {field: 'number', title: __('门票数量')},
                        {field: 'intro', title: __('门票描述')},
                        {field: 'start_time', title: __('开始购票时间'), operate:'RANGE', addclass:'datetimerange',formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
/*                            formatter:function (value,row,index){
                                var that = $.extend({}, this);
                                if(row['type'] ==1){
                                    $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },*/
                            buttons :[
                              {   name: 'execute',
                                    title: __('编辑'),
                                    text: __('编辑'),
                                    url: 'vactivities/ticket/edit',
                                    //icon: 'fa fa-list',
                                    classname: 'btn btn-info btn-xs btn-execute btn-dialog',
                          /*          hidden:function(row){
                                        if(row['status'] == 2 || row['status'] == 0 || (row['startTime'] < row['nowTime'])){
                                            return true;
                                        }

                                    },*/
                                },
                            ]
                        }
                    ],
                ]
            });
            $(document).on("click", ".btn-cancel", function () {
                var data= JSON.stringify(table.bootstrapTable('getSelections'));
                Fast.api.close(data);
            });
            // 获取选中项
     /*       $(document).on("click", ".btn-spec_oper_btn", function () {
                var data= JSON.stringify(table.bootstrapTable('getSelections'));
                var url = $(this).attr('data-url');
                var activity_id = $(this).attr('data-value');
                if(!url) return false;
                Fast.api.ajax({
                    url:url,
                    type:'post',
                    data:{data:data,activity_id:activity_id},
                    dataType: "json",
                }, function(data, ret){
                    //成功的回调
                    Layer.alert(ret.msg);
                    Fast.api.close(data);
                    // table.bootstrapTable('refresh', {});
                    // Layer.close();
                    window.parent.location.reload();
                    // Layer.closeAll();
                    // return false;
                }, function(data, ret){
                    //失败的回调
                    Layer.alert(ret.msg);
                    Fast.api.close(data);
                    window.parent.location.reload();
                    // layer.alert(ret.msg);
                    //return false;
                });
            });*/
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        ticketview:function (){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'vactivities/ticket/ticketlist' + location.search,
                    add_url: 'vactivities/ticket/add',
                    //edit_url: 'vactivities/ticket/edit',
                    //del_url: 'vactivities/ticket/del',
                    //multi_url: 'vactivities/ticket/multi',
                    import_url: 'vactivities/ticket/import',
                    table: 'activity_participants',
                },
                search: true,
                commonSearch: false,
                searchFormVisible: false,
            });

            var table = $("#tablelist");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'name', title: __('门票名称'), operate: 'LIKE'},
                        {field: 'price', title: __('门票价格')},
                        {field: 'number', title: __('门票数量')},
                        {field: 'intro', title: __('门票描述')},
                        {field: 'start_time', title: __('购票时间'), operate:'RANGE', addclass:'datetimerange',formatter: Table.api.formatter.datetime},
                    ],
                ]
            });
            $(document).on("click", ".btn-cancel", function () {
                var data= JSON.stringify(table.bootstrapTable('getSelections'));
                Fast.api.close(data);
            });

            Table.api.bindevent(table);
        },
        ticketadd:function (){

       /*     $(window).load(function() {
               if ($("#is_post").val()=='post_ok'){

               }
            });*/
            Controller.api.bindevent();
  /*              $(document).on("click", ".btn-success", function () {
                    var data= JSON.stringify(table.bootstrapTable('getSelections'));
                    var url = $(this).attr('data-url');
                    var activity_id = $(this).attr('data-value');
                    if(!url) return false;
                    Fast.api.ajax({
                        url:url,
                        type:'post',
                        data:{data:data,activity_id:activity_id},
                        dataType: "json",
                    }, function(data, ret){
                        //成功的回调
                        Layer.alert(ret.msg);
                        Fast.api.close(data);
                        // table.bootstrapTable('refresh', {});
                        // Layer.close();
                        window.parent.location.reload();
                        // Layer.closeAll();
                        // return false;
                    }, function(data, ret){
                        //失败的回调
                        Layer.alert(ret.msg);
                        Fast.api.close(data);
                        window.parent.location.reload();
                        // layer.alert(ret.msg);
                        //return false;
                    });
                });*/
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});